Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Simplify parsing and printing attributes of function and arguments #683

Closed
wants to merge 4 commits into from

Conversation

mununki
Copy link
Member

@mununki mununki commented Oct 15, 2022

Regarding issue rescript-lang/rescript#5735

This PR improves the parsing and printing attributes of functions and arguments so that it makes the implementation simple and clear for future development extensibility.

@mununki mununki marked this pull request as draft October 15, 2022 10:22
@mununki
Copy link
Member Author

mununki commented Oct 15, 2022

Parsing looks okay now, but the printing error is due to an infinite loop.

let f12 = @a (@b x) => 3
let f13 = @a @b (~x) => 3
  structure_item (tests/printer/expr/asyncAwait.res[97,2562+0]..[97,2562+24])
    Pstr_value Nonrec
    [
      <def>
        pattern (tests/printer/expr/asyncAwait.res[97,2562+4]..[97,2562+7])
          Ppat_var "f12" (tests/printer/expr/asyncAwait.res[97,2562+4]..[97,2562+7])
        expression (tests/printer/expr/asyncAwait.res[97,2562+13]..[97,2562+24])
          attribute "a" (tests/printer/expr/asyncAwait.res[97,2562+10]..[97,2562+12])
            []
          Pexp_fun
          Nolabel
          None
          pattern (tests/printer/expr/asyncAwait.res[97,2562+17]..[97,2562+18])
            attribute "b" (tests/printer/expr/asyncAwait.res[97,2562+14]..[97,2562+16])
              []
            Ppat_var "x" (tests/printer/expr/asyncAwait.res[97,2562+17]..[97,2562+18])
          expression (tests/printer/expr/asyncAwait.res[97,2562+23]..[97,2562+24])
            Pexp_constant PConst_int (3,None)
    ]
  structure_item (tests/printer/expr/asyncAwait.res[98,2587+0]..[98,2587+25])
    Pstr_value Nonrec
    [
      <def>
        pattern (tests/printer/expr/asyncAwait.res[98,2587+4]..[98,2587+7])
          Ppat_var "f13" (tests/printer/expr/asyncAwait.res[98,2587+4]..[98,2587+7])
        expression (tests/printer/expr/asyncAwait.res[98,2587+16]..[98,2587+25])
          attribute "a" (tests/printer/expr/asyncAwait.res[98,2587+10]..[98,2587+12])
            []
          attribute "b" (tests/printer/expr/asyncAwait.res[98,2587+13]..[98,2587+15])
            []
          Pexp_fun
          Labelled "x"
          None
          pattern (tests/printer/expr/asyncAwait.res[98,2587+17]..[98,2587+19])
            attribute "ns.namedArgLoc" (tests/printer/expr/asyncAwait.res[98,2587+18]..[98,2587+19])
              []
            Ppat_var "x" (tests/printer/expr/asyncAwait.res[98,2587+17]..[98,2587+19])
          expression (tests/printer/expr/asyncAwait.res[98,2587+24]..[98,2587+25])
            Pexp_constant PConst_int (3,None)
    ]

@cristianoc
Copy link
Contributor

The body returned should be "3" in both cases.

@cristianoc
Copy link
Contributor

Needs to always make progress, and put together a group of args.

@mununki
Copy link
Member Author

mununki commented Oct 15, 2022

expression (tests/printer/expr/asyncAwait.res[97,2562+23]..[97,2562+24])
Pexp_constant PConst_int (3,None)

          expression (tests/printer/expr/asyncAwait.res[97,2562+23]..[97,2562+24])
            Pexp_constant PConst_int (3,None)

I think this is the body returns "3"

Comment on lines +119 to +120
let f12 = @a (@b x) => 3
let f13 = @a @b (~x) => 3
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks printing okay now.

@@ -106,7 +106,7 @@ let _ = await {

let f1 = async (~x, ~y) => x + y
let f2 = async (@foo ~x, @bar ~y) => x + y
let f3 = async (@bar @foo ~x as @zz z, ~y) => x + y
let f3 = @foo async (~x as @bar @zz z, ~y) => x + y
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this expression @attr1 ~x as @attr2 y.
If we parse the attributes inside (...), the attributes are going to be ppat_attributes, the printer emits ~x as @attr1 @attr2 y What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine. There's only one place for attributes on labels.

@mununki mununki marked this pull request as ready for review October 15, 2022 16:58
Js.log("This function should be named 'TopLevel.react'")
ReactDOMRe.createDOMElementVariadic("div", [])
}
@warning("-16")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning is added to pexp_attributes by JSX v3.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsing and printing should not change anything for JSX right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this is printing change. The ast is not changed.

Copy link
Member Author

@mununki mununki Oct 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think the printing changes of JSX output would be a problem unless the ast is changed. What do you think?
I’ll check the js ouput for this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSX is never printed

@mununki
Copy link
Member Author

mununki commented Oct 16, 2022

I ran a test of the compiler with the syntax submodule checked out to this branch. The test result looks fine.

$ make test-all

@@ -1534,12 +1534,6 @@ and parseParameter p =
then
let startPos = p.Parser.startPos in
let uncurried = Parser.optional p Token.Dot in
(* two scenarios:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this comment/mental model to the appropriate place?

Copy link
Member Author

@mununki mununki Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f2a9720 How about this?

EDIT: f902e3a

@mununki
Copy link
Member Author

mununki commented Oct 16, 2022

Rebaed to master to resolve the confict in the test txt

Copy link
Contributor

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Starting to look at this. For post v10.1 release.
Would you rebase on master?

src/res_core.ml Outdated
Asttypes.Labelled lblName,
Ast_helper.Pat.var ~attrs:[propLocAttr] ~loc
Ast_helper.Pat.var ~attrs:([propLocAttr] @ attrs) ~loc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use :: not @

src/res_core.ml Outdated
@@ -1569,25 +1569,29 @@ and parseParameter p =
let pat =
let pat = Ast_helper.Pat.var ~loc (Location.mkloc lblName loc) in
let loc = mkLoc startPos p.prevEndPos in
Ast_helper.Pat.constraint_ ~attrs:[propLocAttr] ~loc pat typ
Ast_helper.Pat.constraint_ ~attrs:([propLocAttr] @ attrs) ~loc pat
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use :: not @

@@ -106,7 +106,7 @@ let _ = await {

let f1 = async (~x, ~y) => x + y
let f2 = async (@foo ~x, @bar ~y) => x + y
let f3 = async (@bar @foo ~x as @zz z, ~y) => x + y
let f3 = @foo async (~x as @bar @zz z, ~y) => x + y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine. There's only one place for attributes on labels.

- of how to parse function parameters and attributes
@mununki
Copy link
Member Author

mununki commented Nov 4, 2022

Rebased to master

@cristianoc
Copy link
Contributor

moved to compiler repo

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants